package com.rmt.www.daoImpl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.rmt.www.dao.RequestDao;
import com.rmt.www.vo.Request;

public class RequestDaoImpl implements RequestDao {

	private JdbcTemplate jdbcTemplate;

	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	public void deleteRequestByRId(int rid) {
		this.jdbcTemplate.update("delete from request where rid=?", new Object[]{rid}, new int[]{java.sql.Types.INTEGER});
	}

	public Request getRequestByRId(int rid) {
		return (Request) this.jdbcTemplate.queryForObject("select* from request where rid=?", 
				new Object[]{rid}, new RequestRowMapper());
	}

	@SuppressWarnings("unchecked")
	public List<Request> getRequestsByPId(int pid) {
		return this.jdbcTemplate.query("select * from request where pid=?", 
				new Object[]{pid}, new int[]{java.sql.Types.INTEGER}, new RequestRowMapper());
	}

	public void insertRequest(Request request) {
		jdbcTemplate.update("insert into request(pid,type,title,authorId,version,priority,source,rational,createdtime,updateUserId,updatedtime,status," +
				"description,cost,effort,risk,updatedtimes,category,aid,requestType) " +
			"values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", 
	new Object[]{
		request.getPid(),
		request.getType(),
		request.getTitle(),
		request.getAuthorId(),
		request.getVersion(),
		request.getPriority(),
		request.getSource(),
		request.getRational(),
		request.getCreatedtime(),
		request.getUpdateUserId(),
		request.getUpdatedtime(),
		request.getStatus(),
		request.getDescription(),
		request.getCost(),
		request.getEffort(),
		request.getRisk(),
		request.getUpdatedtimes(),
		request.getCategory(),
		request.getAid(),
		request.getRequestType()
	},new int[] {
		java.sql.Types.INTEGER,
		java.sql.Types.VARCHAR,
		java.sql.Types.VARCHAR,
		java.sql.Types.INTEGER,
		java.sql.Types.VARCHAR,
		java.sql.Types.VARCHAR,
		java.sql.Types.VARCHAR,
		java.sql.Types.VARCHAR,
		java.sql.Types.TIMESTAMP,
		java.sql.Types.INTEGER,
		java.sql.Types.TIMESTAMP,
		java.sql.Types.VARCHAR,
		java.sql.Types.VARCHAR,
		java.sql.Types.VARCHAR,
		java.sql.Types.INTEGER,
		java.sql.Types.VARCHAR,
		java.sql.Types.INTEGER,
		java.sql.Types.VARCHAR,
		java.sql.Types.INTEGER,
		java.sql.Types.VARCHAR
	});
	}

	

}
